package com.zy.Conterller;

import com.zy.pojo.Posit;

import com.zy.service.PermissionService;
import com.zy.service.PositService;
import com.zy.vo.ResultVo;
import com.zy.vo.TreeNode.TreeNode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sal/posit")
public class PositCount {
    @Resource
    private PositService positService;
    @Resource
    private PermissionService permissionService;



    @GetMapping("/list")
    @RequiresPermissions("posit:list")
    public ResultVo getlist(){
        return ResultVo.success(positService.getRoleList());
    }
    @PostMapping("add")
  @RequiresPermissions("posit:add")
    public ResultVo getRoleadd(@RequestBody Posit role){
        try{
            positService.add(role);
            return ResultVo.success("职位添加成功");
        }catch (Exception e){
            return ResultVo.fail("职位添加失败",e);
        }
    }
    @PostMapping("update")
   @RequiresPermissions("posit:update")
    public ResultVo getRoleupdate(@RequestBody Posit role){
        try{

            role.setRoleDate(new Date());
            positService.update(role);
            return ResultVo.success("职位修改成功");
        }catch (Exception e){
            return ResultVo.fail("职位修改失败",e);
        }
    }
    @GetMapping("del/{id}")
    @RequiresPermissions("posit:del")
    public ResultVo getRoledel(@PathVariable String id){
        try{
            positService.del(Integer.parseInt(id));
            return  ResultVo.success("删除职位成功");
        }catch (Exception e){
            return ResultVo.fail("删除职位失败");
        }
    }
   @GetMapping("/grant")
    @RequiresPermissions("posit:grant")
    public ResultVo grant(Integer roleId){
        List<TreeNode> list=permissionService.getTreeNodes(roleId);
        return ResultVo.success(list);
    }
    @GetMapping("dogrant")
    public ResultVo dogrant(Integer roleId, Integer[] nid){
        try{
            permissionService.dogrant(roleId,nid);
            return ResultVo.success("职位授权成功");
        }catch (Exception e){
            return ResultVo.fail("职位授权失败",e);
        }

    }
    @GetMapping("err/{id}")
    @RequiresPermissions("posit:list")
    public ResultVo geterr(@PathVariable String id){
        try{
            positService.err(Integer.parseInt(id));
            return  ResultVo.success("查询所有职位成功");
        }catch (Exception e){
            return ResultVo.fail("查询所有职位失败");
        }
    }


}
